<?php
namespace App\Models\Traits;
use App\Models\User;
use App\Models\Coupon;
use Illuminate\Support\Carbon;
use App\Notifications\CouponsSend;
trait UserCombineHelper{

    //合并用户
    public static  function combineUser(User $olduser,User $nowuser){
    //把新用户的信息合并至老用户
        $olduser->nickname = $nowuser->nickname;
        if ($nowuser->unionid){
            $olduser->unionid = $nowuser->unionid;
        }
        if ($nowuser->web_openid){
            $olduser->web_openid = $nowuser->web_openid;
        }
        if ($nowuser->mini_openid){
            $olduser->mini_openid = $nowuser->mini_openid;
        }
        if ($nowuser->wx_mini_session_key){
            $olduser->wx_mini_session_key = $nowuser->wx_mini_session_key;
        }
        if ($nowuser->wx_web_session_key){
            $olduser->wx_web_session_key = $nowuser->wx_web_session_key;
        }
        if ($nowuser->avatar){
            $olduser->avatar = $nowuser->avatar;
        }
        //删除新用户的注册礼品券
        Coupon::where(['type'=>2,"user_id"=>$nowuser->id])->delete();
        //老账户保存信息
        $olduser->save();
        //删除新账户
        $nowuser->forceDelete();
        return $olduser;
    }
}
